#include "../Startup/lpc2xxx.h"
#include "../Startup/config.h"
#include "sevenseg.h"
#include "joystick.h"
#include "rgbled.h"
#include "lcd.h"
#include "lcd_hw.h"
#include "utils.h"
#include "timer.h"
#include "rtc.h"
#include "uart1.h"
#include "adc.h"
#include "i2c.h"
#include "eeprom.h"
#include "pca9532.h"
#include "uart.h"

char text[100];
char dirChar[100];

char znak;
char raport[1000];
char title[] = {"REFLEKS RAPORT"};
char results[10][16] =  {
	{"PROBA 01: "},
	{"PROBA 02: "},
	{"PROBA 03: "},
	{"PROBA 04: "},
	{"PROBA 05: "},
	{"PROBA 06: "},
	{"PROBA 07: "},
	{"PROBA 08: "},
	{"PROBA 09: "},
	{"PROBA 10: "}};


int it = 0;
int times[10];
tBool sended;


int main(void)
{
	//Liczba prob
	tU32 count = 5;
	tU32 time_ms = 1000;
	tU32 reflexTime_ms = 0;
	tU8 pca9532Present;

	//Startup

	SegInit();
	//SegTest();

	LedInit();
	//LedTest();

	AdcInit();

	i2cInit();
	pca9532Present = FALSE;
	pca9532Present = pca9532Init();

	/*Uart1SetPinsel();
	Uart1Init();*/
	//initUart0(B9600((CRYSTAL_FREQUENCY * PLL_FACTOR) / VPBDIV_FACTOR), UART_8N1, UART_FIFO_OFF);
	initUart0_96008N1_FifoOff();

	JoySetPinsel();
	JoyInit();

	RtcInit(2012, 11, 8, 12, 0, 0);

	lcdInit();
	lcdContrast(50);
	lcdColor(0xff,0x00);


	//Gameloop
	while(1)
	{
		tU32 level = 0;		
		tU32 dir = 0;
		tBool f = FALSE;
		tU16 adcVal = 0;
		tU8 difficult = 0;
		tU32 cnt = 0;
		
		//Main menu
		tBool exitMainMenu = FALSE;
		tBool resetGame = FALSE;
		
		reflexTime_ms = 0;
		it = 0;
		sended = FALSE;

		lcdClrscr();
		lcdPuts("REFLEKS\n");
		lcdPuts("Potenc. - poziom\n trudnosci\n");
		lcdPuts("Gora - \nrozpoczecie gry\n");

		SegClrAll();

		if (TRUE == pca9532Present)
		{
			setPca9532Pin(0, 1);
			setPca9532Pin(1, 1);
			setPca9532Pin(2, 1);
			setPca9532Pin(3, 1);
			setPca9532Pin(4, 1);
			setPca9532Pin(5, 1);
			setPca9532Pin(6, 1);
			setPca9532Pin(7, 1);
			setPca9532Pin(8, 1);
			setPca9532Pin(9, 1);
			setPca9532Pin(10, 1);
			setPca9532Pin(11, 1);
			setPca9532Pin(12, 1);
			setPca9532Pin(13, 1);
			setPca9532Pin(14, 1);
			setPca9532Pin(15, 1);
		}

		while(!exitMainMenu)
		{
			if(JoyIsUp())
			{
				//x = RtcGetTick();
				exitMainMenu = TRUE;
			}

			adcVal = 3;
			adcVal = AdcGetResult(0);
			difficult = (adcVal / 205) + 1;

			SegSetNumber(difficult);

			switch(difficult)
			{
			case 1:
				count = 5;
				time_ms = 3000;
				break;
			case 2:
				count = 5;
				time_ms = 2000;
				break;
			case 3:
				count = 5;
				time_ms = 1000;
				break;
			case 4:
				count = 10;
				time_ms = 1000;
				break;
			case 5:
				count = 10;
				time_ms = 500;
				break;
			default:
				count = 5;
				time_ms = 3000;
				break;
			}
		}

		//Game begin
		SegClrAll();
		SegSetNumber(5);
		Sleep(1000);

		SegSetNumber(4);
		Sleep(1000);

		SegSetNumber(3);
		Sleep(1000);

		SegSetNumber(2);
		Sleep(1000);

		SegSetNumber(1);
		Sleep(1000);

		SegSetNumber(0);
		Sleep(1000);

		SegClrAll();

		//Game
		level = 0;
		while(level < count)
		{
			f = FALSE;

			//wybranie segmentu do wywietlenia
			dir = 1 + (genRand() % 4);

			Sleep(500);
			LedSet(BLUE);
			SegClrAll();


			if(dir == 1)
			{
				SegSetDirection(UP);
			}
			else if(dir == 2)
			{
				SegSetDirection(DOWN);
			}
			else if (dir == 3)
			{
				SegSetDirection(LEFT);
			}
			else if (dir == 4)
			{
				SegSetDirection(RIGHT);
			}
			else
			{
				SegSetDirection(UP);
			}

			T1Start(time_ms);

			//sprawdzamy czy czas minal
			while (T1Stopped())
			{
				if( (JoyIsUp()    &&  (dir == 1) && !JoyIsDown() && !JoyIsLeft() && !JoyIsRight()) ||
					(JoyIsDown()  &&  (dir == 2) && !JoyIsUp() && !JoyIsLeft() && !JoyIsRight()) ||
					(JoyIsLeft()  &&  (dir == 3) && !JoyIsDown() && !JoyIsUp() && !JoyIsRight()) ||
					(JoyIsRight() &&  (dir == 4) && !JoyIsDown() && !JoyIsLeft() && !JoyIsUp())  )
				{
					f = TRUE;
					//reflexTime_ms += T1ElapsedTimeMs();
					break;
				}
			}

			if(f == FALSE)
			{
				LedSet(RED);
			}
			else
			{
				if (TRUE == pca9532Present)
				{
					setPca9532Pin(level, 0);
				}

				LedSet(GREEN);
			}

			reflexTime_ms += T1ElapsedTimeMs();
			times[it] = T1ElapsedTimeMs();
			it++;
			level++;
		}

		//Statistics
		reflexTime_ms /= count;
		intToChar(reflexTime_ms, text);
		lcdClrscr();
		lcdPuts("Sredni czas\n reakcji:");
		lcdPuts("\n");
		lcdPuts(text);
		lcdPuts(" ms\n");
		//x = reflexTime_ms;

		SegClrAll();

		//send scores
		{
			
			cnt = 0;
			int i, j;
			char time[5];

			//copy title
			for(i = 0; i < sizeof(title) - 1; i++)
			{
				raport[cnt] = title[cnt];
				cnt++;
			}

			raport[cnt++] = '\n';


			//generate results
			for(i = 0; i < count; i++)
			{
				intToChar(times[i], time);

				for(j = 10; j < 15; j++)
				{
					results[i][j] = time[j - 10];
				}

				results[i][j] = '\n';

				for(j = 0; j < 16; j++)
				{
					if(results[i][j] == '\0')
					{
						results[i][j] = ' ';
						//break;
					}
					raport[cnt++] = results[i][j];
				}
			}

			raport[cnt++] = '\0';

			//send
			/*Uart1SetPinsel();
			if(XmSendData(raport, cnt))
			{
				lcdPuts("Wysylanie zakonczone\n");
			}
			JoySetPinsel();*/
		}


		lcdPuts("Dol - main menu\n");
		lcdPuts("Prawo - wyslij\n raport\n");


		//Reset game
		while(!resetGame)
		{
			if(sended == FALSE)
			{
				if(JoyIsRight())
				{
					uart0SetPinsel();
					//uart0SendChar('a');
					//uart0GetCh();

					uart0SendChars(raport, cnt);

					//if(XmSendData(raport, cnt))
					{
						lcdPuts("Wysylanie zakonczone\n");
						sended = TRUE;
					}
					JoySetPinsel();
				}
			}

			if(JoyIsDown())
			{
				resetGame = TRUE;
			}

			LedSet(RED);
			Sleep(100);
			LedSet(GREEN);
			Sleep(100);
			LedSet(BLUE);
			Sleep(100);

		}
	}

  return 0;
}
